<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$return['wechats']=pdo_getall('core_account_wechat',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
if($_RQ['op']=='list'){
	//搜索
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if(!empty($_RQ['wxgid'])){
		$params['wxgid']=intval($_RQ['wxgid']);
		$return['params']['wxgid']=$_RQ['wxgid'];
	}
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	$fields=array('id','tag_id','title','wxgid');
	$pindex = max(1, intval($_RQ['page']));
	$psize = 15;
	$total='';
	$list=pdo_getslice('core_fansgroup',$params,array($pindex,$psize),$total,$fields);
	if($list){
		foreach($list as &$row){
			$row['fansnum']=pdo_count('core_fans',array('uniacid'=>$_SESSION['uniacid'],'wxgid'=>$row['wxgid'],'tagid_list like'=>'%;'.$row['tag_id'].';%'));
		}
	}
	$return['list']=$list?$list:array();
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'粉丝组列表'),
			array('op'=>'get','title'=>'同步粉丝组'),
		),
		'list'=>array('data'=>'list','delall'=>false,'list'=>array(
			array('name'=>'title','title'=>'粉丝组名称','type'=>'string'),
			array('name'=>'wxgid','title'=>'所属公众号','type'=>'select','options'=>'wechats'),
			array('name'=>'tag_id','title'=>'粉丝组ID','type'=>'string'),
			array('name'=>'fansnum','title'=>'用户数量','type'=>'string'),
		)),
		'search_one'=>array('name'=>'title','select_name'=>'wxgid','select_title'=>'全部公众号','options'=>'wechats'),
	);
	$return['htmls']=$htmls;
	exi($return);
}

if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('core_fansgroup',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));

	}else{
		$item=array();
	}
	
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'desc'=>'粉丝组详情',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回列表'),
		),
		'edit'=>array(
			array('type'=>'string','title'=>'粉丝组名称','name'=>'title', 'help'=>'' ),
		)
	);
	if(!$item){
		$htmls['edit'][]=array('type'=>'select','name'=>'wxgid','title'=>'选择公众号','options'=>'wechats');
	}else{
		$item['wechat_title']=$return['wechats'][$item['wxgid']]['title'];
		unset($return['wechats']);
		$htmls['edit'][]=array('type'=>'string','name'=>'wechat_title','title'=>'所属公众号','read'=>true);
	}
	$return['item']=$item;
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$data=array(
		'title'=>trim($postdata['title']),
	);
	$id=intval($postdata['id']);
	if($id){
		$old=pdo_get('core_fansgroup',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		if(!$old){
			exi('指定粉丝组不存在或已删除！','error');
		}
		if($old['title']!=$data['title']){
			$tag=array('id'=>$old['tag_id'],'name'=>$data['title']);
			cfc('account',$old['wxgid'])->edit_tag($tag);
			pdo_update('core_fansgroup',$data,array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
		}
		
		exi('粉丝组修改成功！','','list');
	}else{
		
		$data['wxgid']=intval($postdata['wxgid']);
		if(!$data['wxgid']){
			exi('请先选择公众号！','error');
		}
		$tag_id=cfc('account',$data['wxgid'])->add_tag($data['title']);
		if(!$tag_id){
			exi('创建粉丝组失败！','error');
		}
		$data['tag_id']=$tag_id;
		$data['uniacid']=$_SESSION['uniacid'];
		pdo_insert('core_fansgroup',$data);
		exi('粉丝组增加成功！','','list');
	}
}

if($_RQ['op'] == 'del'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要删除的粉丝组！','error');
	}
	$tag=pdo_get('core_fansgroup',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	if(!$tag){
		exi('指定的粉丝组不存在或已删除！','error');
	}
	cfc('account',$tag['wxgid'])->del_tag($tag['tag_id']);
	pdo_delete('core_fansgroup',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	exi('删除成功！');
}
if($_RQ['op'] == 'get'){
	//同步粉丝组
	if($_RQ['postdata']){
		$post=get_postdata();
		$wxgid=intval($post['wxgid']);
		if(!$wxgid){
			exi('请先选择要同步的公众号！','error');
		}
		$tags=cfc('account',$wxgid)->get_tag();
		foreach($tags as $tag){
			$check=pdo_get('core_fansgroup',array('uniacid'=>$_SESSION['uniacid'],'wxgid'=>$wxgid,'tag_id'=>$tag['id']));
			if($check){
				if($check['title']!=$tag['name']){
					pdo_update('core_fansgroup',array('title'=>$tag['name']),array('id'=>$check['id']));
				}
			}else{
				$insert=array(
					'uniacid'=>$_SESSION['uniacid'],
					'wxgid'=>$wxgid,
					'tag_id'=>$tag['id'],
					'title'=>$tag['name'],
				);
				pdo_insert('core_fansgroup',$insert);
			}
		}
		exi('同步成功！','','list');
	}
	$return['item']=array();
	$return['htmls']=array(
		'type'=>'edit',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回列表'),
		),
		'edit'=>array(
			array('type'=>'select','name'=>'wxgid','title'=>'选择公众号','options'=>'wechats'),
		),
	);
	exi($return);
}

?>